<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="48">

          <a-col :md="8" :sm="24">
            <a-form-item label="场地物资名称">
              <a-input v-model="queryParam.materialName" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <a-form-item label="申请人">
              <a-input v-model="queryParam.realname" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <a-form-item label="开始时间">
              <a-date-picker v-model="queryParam.startTime" style="width: 100%" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <a-form-item label="结束时间">
              <a-date-picker v-model="queryParam.endTime" style="width: 100%" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :md="(!advanced && 6) || 24" :sm="24">
            <span
              class="table-page-search-submitButtons"
              :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
            >
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            
            </span>
          </a-col>

          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="handleApply">申请场地和物资</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" v-has="'Material:delete'"/>批量删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <s-table ref="table" size="default" :scroll="{x:1000}" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}" :columns="columns" :data="loadDataList" @onSelect="onChange">
      <span slot="action" slot-scope="text, record">
        <a @click="handleReview(record)">查看</a>
        <a @click="handleEdit(record)" v-has="'Material:edit'" v-if="record.actStatus!=10" style="margin-left:10px">编辑</a>
        <a-popconfirm v-has="'Material:delete'" style="margin-left:10px" title="确定删除?" @confirm="() => handleDelete(record.id)">
          <a>删除</a>
        </a-popconfirm>
        <a style="margin-left:10px" @click="handleDeal(record)" v-if="record.dealFlag">审批</a>
      </span>
    </s-table>

    <!-- 申请弹窗 -->
    <MaterialApplyModal ref="MaterialApplyModal" @ok="searchQuery"></MaterialApplyModal>

    <!-- 审批 -->
    <Approval
      v-model="shenpi"
      :bizId="mdl.id"
      :businessUrl="url.add"
      detailComponent="MaterialApplyForm"
      @ok="searchQuery"
    ></Approval>
    <!-- 查看 -->
    <CheckModal :bizId="detail.id" bizComponent="/material/modules/MaterialApplyForm" v-model="detail.visible" ref="CheckModal"></CheckModal>
  
  </a-card>
</template>

<script>
import STable from '@/components/table/'
import XSelectUserByDep from '@/components/extends/XSelectUserByDep'
import XImport from '@/components/extends/XImport.vue'
import ATextarea from 'ant-design-vue/es/input/TextArea'
import AInput from 'ant-design-vue/es/input/Input'
import moment from 'moment'
import {merge} from 'lodash'
import { getMaterialApplys ,setMaterialApply,delMaterialApply,delMaterialApplys} from '@/api/api'
import {getAction,postAction, deleteAction } from '@/api/manage'
import MaterialApplyModal from './modules/MaterialApplyModal'
import Approval from '@/components/Approval/Approval'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import CheckModal from '@/components/extends/CheckModel'
export default {
  name: 'TableList',
  components: {
    CheckModal,
    AInput,
    ATextarea,
    STable,
    XImport,
    Approval,
    XSelectUserByDep,
    MaterialApplyModal
  },
  mixins:[JeecgListMixin],
  data() {
    return {
      detail:{},
      disableMixinCreated:true,
      shenpi:false,
      visibleImport:false,
      toggleSearchStatus:false,
      form: null,
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
      },
      url:{
        add:'/material/add',
        exportXlsUrl:'',
        deleteBatch:'/material/deleteBatch'
      },
      // 表头
      columns: [
        {
          title: '活动主题',
          align: 'center',
          dataIndex: 'theme',
          width:180,
          ellipsis: true,
        },
        {
          title: '活动形式',
          align: 'center',
          dataIndex: 'style',
          width:150
        },
        {
          title: '申请场地和物品',
          align: 'center',
          dataIndex: 'detailNames',
          width:220,
          ellipsis: true,
        },
        {
          title: '申请组织',
          align: 'center',
          dataIndex: 'applyOrgName',
          width:180
        },
        {
          title: '学生负责人',
          align: 'center',
          dataIndex: 'student',
          width:150
        },
        {
          title: '负责人电话',
          align: 'center',
          dataIndex: 'studentPhone',
          width:150
        },
        {
          title: '使用时间',
          align: 'center',
          dataIndex: 'startTime',
          width:420,
          customRender(text, record, index){
            return record.startTime + ' - ' +record.endTime
          }
        },
        {
          title: '归还时间',
          align: 'center',
          dataIndex: 'returnTime',
          width:280,
        },
        {
          title: '申请理由',
          align: 'center',
          dataIndex: 'reason',
          width:320,
          ellipsis: true,
        },
        {
          title: '审批状态',
          align: 'center',
          dataIndex: 'actStatus_dictText',
          width:150,
        },
        {
          title: '当前审批人',
          align: 'center',
          dataIndex: 'waitHandler',
          width:180,
        },
        {
          title: '操作',
          align: 'center',
          fixed:"right",
          width: 250,
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadDataList: parameter => {
        return getMaterialApplys(Object.assign(parameter, this.queryParam)).then(res => {
          if(res.success&&res.result){
            const obj = {
              data: res.result.records||[],
              pageNo: res.result.current,
              pageSize: res.result.size,
              totalCount: res.result.total,
              totalPage: res.result.page,
              sortColumns: res.result.sortColumns
            }
            return obj
          }else{
            return {}
          }
        })
      },

      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    handleAdd(){
      this.$refs.MaterialApplyModal.title = '添加'
      this.$refs.MaterialApplyModal.add();
    },
    handleEdit(record){
      if(record.actStatus==10){
        this.$refs.MaterialApplyModal.returnTimeEditable = true
      }else{
        this.$refs.MaterialApplyModal.returnTimeEditable = false
      }
      this.$refs.MaterialApplyModal.title = '编辑'
      this.$refs.MaterialApplyModal.edit(record,{
        applierId:true
      });
    },
    handleReview(record){
      // this.$refs.MaterialApplyModal.review(record);
      this.detail = record
      this.$set(this.detail,'visible',true)
    },
    handleToggleSearch(){
      this.toggleSearchStatus = !this.toggleSearchStatus;
    },
    // 申请使用
    handleApply(record){
      this.$refs.MaterialApplyModal.title = '场地和物资申请'
      this.$refs.MaterialApplyModal.create({
        materialId:record.id,
        materialName:record.name,
        code:record.code,
        type:record.type,
        applierId: this.$store.getters.userInfo.id,
        realname: this.$store.getters.userInfo.realname,
      },{
        applierId:true
      });
    },
    /**
     * 处理
     */
    handleDeal(record) {
      this.mdl = Object.assign({}, record)
      this.shenpi = true
    },
    handleDelete(id){
      delMaterialApply({id}).then(res =>{
          if(res.success){
            this.$message.success('删除成功');
            this.searchQuery()
          }else{
            this.$message.error(res.message)
          }
      })
    },
    batchDel: function () {
      if(!this.url.deleteBatch){
        this.$message.error("请设置api.leteBatch属性!")
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！');
        return;
      } else {
        var ids = "";
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ",";
        }
        var that = this;
        this.$confirm({
          title: "确认删除",
          content: "是否删除选中数据?",
          onOk: function () {
            that.loading = true;
            deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.selectedRowKeys = [];
                that.selectionRows = [];
                that.searchQuery()
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.loading = false;
            });
          }
        });
      }
    },
    searchReset() {
      this.queryParam = {
      }
      this.searchQuery();
    },
    searchQuery() {
      this.$nextTick(() => {
        this.$refs.table.refresh()
      })
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
    },
    onChange(row) {
      this.selectedRowKeys = row.selectedRowKeys
      this.selectedRows = row.selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
